package org.example.fanruan;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class MinMove {
    private static final int N = 202;

    private int n, l, r;
    private int[] a = new int[N];
    private int[] b = new int[N];
    private int sum;
    private int num;
    private int answer = Integer.MAX_VALUE;

    public void solution() throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileInputStream("1.in"));
        n = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = scanner.nextInt();
            sum += a[i];
        }
        sum /= n;
        for(int i = 1; i <= n; i++) {
            a[i] -= sum;
            a[i + n] = a[i];
        }
        for(int i = 1; i <= n; i++) {
            num = 0;
            l = i;
            r = i + n - 1;
            for(int j = l; j <= r; j++) {
                b[j] = a[j];
            }
            while(l < r) {
                if(b[l] == 0) {
                    l++;
                    continue;
                }
                num += Math.abs(b[l]);
                b[l + 1] += b[l];
                b[l] = 0;
            }
            if(num < answer) {
                answer = num;
            }
        }
        System.out.println(answer);
    }

    public static void main(String[] args) throws FileNotFoundException {
        new MinMove().solution();
    }
}